package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.exception.LoginFailedException;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.mapper.UserMapper;
import com.sky.properties.JwtProperties;
import com.sky.properties.WeChatProperties;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: UserServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/9/12 14:14
 * @Version 1.0
 */
@Service
public class UserServiceImpl  implements UserService {
    public static final String WECHAT_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WeChatProperties weChatProperties;
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        //调用微信接口服务获取当前用户的openId
        String openId = getOpenId(userLoginDTO.getCode());
//        Map<String, String> map = new HashMap<>();
//        map.put("appid",weChatProperties.getAppid());
//        map.put("secret",weChatProperties.getSecret());
//        map.put("js_code",userLoginDTO.getCode());
//        map.put("grant_type","authorization_code");
//        String josn = HttpClientUtil.doGet(WECHAT_LOGIN_URL, map);
//        JSONObject jsonObject = JSON.parseObject(josn);
//        String openid = jsonObject.getString("openid");
        //判断openId是否为空，如果为空表示登陆失败，抛出异常

        if (openId == null){
            throw  new LoginFailedException(MessageConstant.LOGIN_FAILED);
        }
        //判断当前用户是否为新用户
        User user = userMapper.selectByOpenid(openId);

        //如果是新用户，自动完成注册
        if (user == null){
          user = User.builder()
                    .openid(openId)
                    .createTime(LocalDateTime.now()).build();

          user =  userMapper.insert(user);
        }
        //返回这个用户对象


        return user;
    }

    /**
     * 调用微信接口服务来获取用户的OpenID
     * @param code
     * @return
     */

    private  String getOpenId(String code){
        //调用微信接口服务获取当前用户的openId
        Map<String, String> map = new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        // TODO 为了api好调用直接返回OpenId
        String openid = "o4mWf6GI63loQhO3cH5WmuYvjeWA";
//        String josn = HttpClientUtil.doGet(WECHAT_LOGIN_URL, map);
//        JSONObject jsonObject = JSON.parseObject(josn);
//        String openid = jsonObject.getString("openid");
        return openid;
    }
}
